<?php

/*
info: 系统公用调用处理文件
date: 2011-12-15
*/

set_magic_quotes_runtime(0);
@ini_set('memory_limit', '32M');
$db = '';	//数据库类对象预定义
$user = array();	//用户信息数组
$webcache = array();
$webglobal = array();
$webserver = array();
$webcookie = array();
$webupfile = array();
$webrequest = array();
date_default_timezone_set('Asia/Chongqing');
$webserver['beginmtime'] = explode(' ', microtime());
$webserver['timestamp'] = time();

define('WEBINROOT', TRUE);
define('WEBFILEPATH', substr(dirname(__FILE__), 0, -8));
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());

if (PHP_VERSION < '4.1.0')
{
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}

require WEBFILEPATH.'/config.inc.php';
require WEBFILEPATH.'/include/global.func.php';
require WEBFILEPATH.'/include/cache.func.php';
require WEBFILEPATH.'/include/template.func.php';
require WEBFILEPATH.'/include/mysql.class.php';

//获取参数处理
foreach (array('_POST', '_GET') as $temprequest)
{
	foreach ($$temprequest as $tempkey => $tempvalue)
	{
		$webrequest[$tempkey] = global_Addslashes($tempvalue);
	}
}
unset($temprequest, $HTTP_GET_VARS, $_GET, $HTTP_POST_VARS, $_POST);

$tempprelength = strlen($webconfig['cookiepre']);
foreach ($_COOKIE as $tempkey => $tempvalue)
{
	if (substr($tempkey, 0, $tempprelength) == $webconfig['cookiepre'])
	{
		$webcookie[(substr($tempkey, $tempprelength))] = global_Addslashes($tempvalue);
	}
}
unset($tempprelength, $tempkey, $tempvalue, $HTTP_COOKIE_VARS, $_COOKIE);

if (!empty($_FILES))
{
	$webupfile = global_Addslashes($_FILES);
}
unset($HTTP_POST_FILES, $_FILES, $_REQUEST);

//系统定义参数
$webglobal['htmltitle'] = $webglobal['htmldescription'] = '';
$webglobal['htmlkeyword'] = $webglobal['htmlhead'] = $webglobal['htmlnavigation'] = array();
$webglobal['page'] = array();
$webglobal['encodetime'] = urlencode(global_EncryptKey($webserver['timestamp'], WEBAUTHKEY));

$webserver['showtime'] = global_FormatTime($webserver['timestamp']);
$webserver['webhost'] = $_SERVER['HTTP_HOST'];
$webserver['runfile'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$webserver['getstr'] = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ((isset($_SERVER['argv']) && !empty($_SERVER['argv'])) ? $_SERVER['argv'][0] : '');
$webserver['geturl'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $webserver['runfile'].(!empty($webserver['getstr']) ? '?'.$webserver['getstr'] : '');
$webserver['comeurl'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$webserver['pageurl'] = 'http://'.$webserver['webhost'].$webserver['geturl'];

// 用户IP获取
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
{
	$user['ip'] = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
{
	$user['ip'] = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
{
	$user['ip'] = getenv('REMOTE_ADDR');
}
elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
{
	$user['ip'] = $_SERVER['REMOTE_ADDR'];
}
preg_match("/[\d\.]{7,15}/", $user['ip'], $tempipmatches);
$user['ip'] = $tempipmatches[0] ? $tempipmatches[0] : 'unknown';
unset($tempipmatches);

//载入全局设定
cache_GetSetting();

//前台共用数据读取
if (!defined('ADMIN_WEBINROOT'))
{
	include_once WEBFILEPATH.'/include/category.class.php';
	$webglobal['action'] = isset($webrequest['action']) ? trim($webrequest['action']) : '';
	$webrequest['page'] = isset($webrequest['page']) ? max(1, intval($webrequest['page'])) : 1;

	//城市和区域
	$areaDataModel = CategoryDataModel::factory('area', array('cache' => true));
	$webglobal['areadata'] = $areaDataModel->formatCategoryOption(0,0);
	$webglobal['citydata'] = $areaDataModel->getCategoryForLevel(1);
	$webglobal['curcity'] = isset($webrequest['city']) ? trim($webrequest['city']) : (isset($webcookie['city']) ? trim($webcookie['city']) : '');
	$webglobal['curcityid'] = 0;
	$webglobal['curcityshow'] = '全国';
	if ($webglobal['curcity'] != '')
	{
		$webglobal['curcityid'] = global_SearchArray($webglobal['curcity'], $webglobal['citydata'], 'short_english');
		if ($webglobal['curcityid'] === FALSE)
		{
			$webglobal['curcity'] = '';
			$webglobal['curcityid'] = 0;
		}
		else
		{
			$webglobal['curcityshow'] = $webglobal['citydata'][$webglobal['curcityid']]['name'];
			if (!isset($webcookie['city']) || $webcookie['city']!=$webglobal['curcity'])
			{
				global_SetCookie('city', $webglobal['curcity']);
			}
		}
	}
	if ($webglobal['curcityid']==0) {
		if ($webglobal['citydata']) {
			if ($defaultCityID = global_SearchArray('tianjin', $webglobal['citydata'], 'short_english')) {
				$currentCityData = $webglobal['citydata'][$defaultCityID];
			} else {
				$currentCityData = current($webglobal['citydata']);
			}
			$webglobal['curcity'] = $currentCityData['short_english'];
			$webglobal['curcityid'] = $currentCityData['id'];
			$webglobal['curcityshow'] = $currentCityData['name'];
		} else {
			global_ClearCookie('city');
		}
	}

	//读取商家分类
	$companyCategoryDataModel = CategoryDataModel::factory('company', array('cache' => true));
	$webglobal['companyCategory'] = array();

	if ($categories = $companyCategoryDataModel->getChildForLayer(0, 2)) {
		foreach ($categories as $categoryid => $category) {
			if (isset($webglobal['companyCategory'][$category['upid']])) {
				$webglobal['companyCategory'][$category['upid']]['second'][$category['id']] = $category;
			} else {
				$webglobal['companyCategory'][$category['id']] = $category;
				$webglobal['companyCategory'][$category['id']]['second'] = array();
			}
		}
	}
	
	//读取优惠劵分类
	$couponCategoryDataModel = CategoryDataModel::factory('coupon', array('cache' => true));
	$webglobal['couponCategory'] = $couponCategoryDataModel->getChildForLayer(0, 2);

	//用户登录
	$user['base_uid'] = 0;	//设定当前用户ID为0
	if (defined('USER_LOGIN_CHECK'))
	{
		include_once WEBFILEPATH.'/include/user.class.php';
		$usercheckobj = new User();
		$usercheckobj->CheckUser();
	}

	//站点关闭
	if (isset($webcache['setting']['siteclosed']) && $webcache['setting']['siteclosed']==1)
	{
		global_ShowMessage($webcache['setting']['closedreason']);
	}
}
